package com.fedming.bottomnavigationdemo.Adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedming.bottomnavigationdemo.R;

import java.util.List;

public class NewsRecycleViewAdapter extends RecyclerView.Adapter<NewsRecycleViewAdapter.ViewHolder> {
    private List<News> mNewsList;//数据源

    public NewsRecycleViewAdapter(List<News> list) {
        mNewsList = list;
    }

    @Override
    public NewsRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_recycle_item, parent, false);
        final NewsRecycleViewAdapter.ViewHolder holder = new NewsRecycleViewAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(NewsRecycleViewAdapter.ViewHolder holder, int position) {
        News news = mNewsList.get(position);
        holder.icon.setImageResource(news.getImageId());
        holder.time.setText(news.getTime());
        holder.content.setText(news.getContent());
        holder.from.setText(news.getFrom());

    }

    public void removeData(int position) {
        mNewsList.remove(position);
        notifyItemRemoved(position);
    }
    public void addData(int position,News news) {
        mNewsList.add(position, news);
        notifyItemInserted(position);
    }

    //更改某个位置的数据
    public void changeData(int position,News news) {
        mNewsList.set(position, news);
        notifyItemChanged(position);
    }


    @Override
    public int getItemCount() {
        return mNewsList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View newsView;
        ImageView icon;
        TextView time;
        TextView content;
        TextView from;
        public ViewHolder(View view) {
            super(view);
            newsView = view;
            icon = (ImageView) view.findViewById(R.id.news_icon);
            time = (TextView) view.findViewById(R.id.advice_time);
            content = (TextView) view.findViewById(R.id.advice_content);
            from = (TextView) view.findViewById(R.id.from);
        }

    }

}
